package com.slz3.business.module.memberManagement.memThirdLoginInfo.steward;

import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.ConvertUtil;
import com.slz3.business.frame.util.RedisClient;
import com.slz3.business.module.memberManagement.memThirdLoginInfo.ApiVo.ThirdUserVo;
import com.slz3.business.module.memberManagement.memThirdLoginInfo.domain.MemThirdLoginInfo;
import com.slz3.business.module.memberManagement.memThirdLoginInfo.enums.MemThirdLoginInfoSexFlag;
import com.slz3.business.module.memberManagement.memThirdLoginInfo.enums.MemThirdLoginInfoTypeFlag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @FileName: MemThirdLoginInfoBackSync
 * @Description: 同步第三方用户信息
 */

@Component
@Slf4j
public class MemThirdLoginInfoBackSync {

    /**
     * 会员抽奖券同步
     */
    public static void syncData(){

        try {
            Object obj = Jedis.db0.leftPop(RK.User_THIRD);
            Map<String, Object> dataMap = (Map<String, Object>) obj;
            MemThirdLoginInfo entity = setMemeThirdLoginInfo(dataMap);
            log.info("消费消息:" + obj.toString());
            //处理完删除set集合
            Services.memThirdLoginInfo.save(entity);
            RedisClient.redisTemplate.opsForSet().pop(RK.User_THIRD+"Set");
        }catch (Exception e){
            //主数据set 集合
            Set userScore= Jedis.db0.getSet(RK.SAVE_ORDER.concat("Set"));
            Iterator iterator = userScore.iterator();
            //消费逻辑
            log.info("消费消息:" + iterator.toString());
            //遍历set集合
            while(iterator.hasNext()){
                Map<String, Object> dataMap =(Map<String,Object>) iterator.next();
                MemThirdLoginInfo entity = setMemeThirdLoginInfo(dataMap);
                Steward.initNullField(entity);
                //根据map先获取订单信息并保存订单
                Services.memThirdLoginInfo.save(entity);;
            }
            //删除set
            RedisClient.redisTemplate.opsForSet().pop(RK.User_THIRD+"Set");
        }

    }

    /**
     * 组装第三方用户实体
     * @param dataMap
     * @return
     */
    private static MemThirdLoginInfo setMemeThirdLoginInfo(Map<String, Object> dataMap) {
        MemThirdLoginInfo vo = new MemThirdLoginInfo();
        ThirdUserVo thirdUserVos = ConvertUtil.objToObj(dataMap, ThirdUserVo.class);
        vo.setUuid(UUID.fromString(thirdUserVos.getId()));
        vo.setSerOpenid(thirdUserVos.getOpenid());
        vo.setSerUsing(UsingFlag.ENABLE);
        vo.setSerPicture(thirdUserVos.getPicture());
        // 设置性别
        Integer sex = thirdUserVos.getSex();
        if(sex == 1){
            vo.setSerSex(MemThirdLoginInfoSexFlag.NAN);
        }else if(sex == 2){
            vo.setSerSex(MemThirdLoginInfoSexFlag.NV);
        }else {
            vo.setSerSex(MemThirdLoginInfoSexFlag.WEIZHI);
        }
        vo.setSerUserName(thirdUserVos.getUserName());
        Integer type = thirdUserVos.getType();
        //设置登录类型  0：支付宝 1： 微信 3：微博
        if(type == 0){
            vo.setSerType(MemThirdLoginInfoTypeFlag.ZHIFUBAO);
        }else if(type == 1){
            vo.setSerType(MemThirdLoginInfoTypeFlag.WEIXIN);
        }else {
            vo.setSerType(MemThirdLoginInfoTypeFlag.WEIXBO);
        }
        return  vo;
    }


}